package cn.xspace.module.other.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import cn.xspace.module.other.R;
import cn.xspace.module.other.activity.PlaceSetAct;
import cn.xspace.module.other.util.RProxy;
import cn.xspace.wnx.entity.AreaBean;
import cn.xspace.wnx.util.ShareUtils;

public class PlaceSetAdapter extends BaseAdapter {

//    public List<PlaceSetBean> mList = new ArrayList<PlaceSetBean>();
    public List<AreaBean> mList = new ArrayList<>();
    Context context;
    boolean isShowCheck;
    PlaceSetAct act;
    private String lastPage;
    private String page1;
    private String page2;
    private String page3;
    private String page4;

    public PlaceSetAdapter(PlaceSetAct act, List<AreaBean> mList, boolean isShowCheck) {
        super();
        this.mList = mList;
        this.act = act;
        context = (Context) act;
        this.isShowCheck = isShowCheck;
        lastPage = (String) ShareUtils.get(context, "lastPage", "");
        page1 = (String) ShareUtils.get(context, "page0", "");
        page2 = (String) ShareUtils.get(context, "page1", "");
        page3 = (String) ShareUtils.get(context, "page2", "");
        page4 = (String) ShareUtils.get(context, "page3", "");
    }

    public PlaceSetAdapter(Context context, List<AreaBean> list) {
        this.context = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return this.mList.size();
    }

    @Override
    public Object getItem(int index) {
        return this.mList.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.place_set_item_new, null);
        if (!"0".equalsIgnoreCase(mList.get(position).isLeaf)) {
            convertView.findViewById(RProxy.img2).setVisibility(View.GONE);
        }
        CheckBox cb = (CheckBox) convertView.findViewById(R.id.item_placeset_check);
        ImageView img_icon = (ImageView) convertView.findViewById(R.id.img1);
        if (isShowCheck) {
            cb.setVisibility(View.VISIBLE);
            img_icon.setVisibility(View.GONE);
            String areacode = mList.get(position).area_code;
            LogUtil.e("PlaceSetAdapter  " + areacode + "--" + lastPage + "--" + page1 + "--" + page2 + "--" + page3);
            if (areacode.equals(page1)) cb.setChecked(true);
            if (areacode.equals(page2)) cb.setChecked(true);
            if (areacode.equals(page3)) cb.setChecked(true);
            if (areacode.equals(page4)) cb.setChecked(true);
            if (areacode.equals(lastPage)) cb.setChecked(true);

            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        AreaBean setbean = (AreaBean) getItem(position);
                        act.place = setbean.area_name;
                        act.place_id = setbean.area_code;
                        LogUtil.e(act.place + " ==========="  + mList.get(position).area_code);
                        ShareUtils.put(context,"page0",mList.get(position).area_code+"");
                        act.returnType();
                    }

                }
            });
        } else {
            cb.setVisibility(View.GONE);
            img_icon.setVisibility(View.VISIBLE);
            cb.setOnCheckedChangeListener(null);
        }
        TextView tv_place = (TextView) convertView.findViewById(RProxy.tv_place);
        tv_place.setText(mList.get(position).area_name);

        return convertView;
    }

}
